package com.backend.demo1.services;

import com.backend.demo1.mapper.PermissionMapper;
import com.backend.demo1.mapper.RoleMapper;
import com.backend.demo1.mapper.RolePermissionMapper;
import com.backend.demo1.models.Permission;
import com.backend.demo1.models.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public void run(String... args) throws Exception {
        // 初始化权限
        List<Permission> permissions = initializePermissions();

        // 初始化角色并分配权限
        initializeRoles(permissions);
    }

    private List<Permission> initializePermissions() {
        List<Permission> permissions = new ArrayList<>();
        // 定义需要初始化的权限
        String[] permissionNames = {"create_user", "edit_user", "delete_user", "view_user", "manage_roles"};
        for (String name : permissionNames) {
            // 检查权限是否已存在
            Permission existingPermission = permissionMapper.findByName(name);
            if (existingPermission == null) {
                Permission permission = new Permission(name, name + " permission");
                permissionMapper.insert(permission); // 插入新权限
                permissions.add(permission);
            } else {
                permissions.add(existingPermission);
            }
        }

        return permissions;
    }

    private void initializeRoles(List<Permission> permissions) {
        // 定义需要初始化的角色及其对应的权限
        Map<String, List<Permission>> rolePermissionsMap = new HashMap<>();

        // guest 角色没有任何权限
        List<Permission> guestPermissions = new ArrayList<>(); // 创建空的权限列表
        rolePermissionsMap.put("guest", guestPermissions);

        // user 角色拥有 view_user 权限
        List<Permission> userPermissions = new ArrayList<>();
        for (Permission permission : permissions) {
            if ("view_user".equals(permission.getName())) {
                userPermissions.add(permission);
                break;
            }
        }
        rolePermissionsMap.put("user", userPermissions);

        // coach 角色拥有 view_user, edit_user, create_user 权限
        List<Permission> coachPermissions = new ArrayList<>();
        for (Permission permission : permissions) {
            if ("view_user".equals(permission.getName()) || 
                "edit_user".equals(permission.getName()) || 
                "create_user".equals(permission.getName())) {
                coachPermissions.add(permission);
            }
        }
        rolePermissionsMap.put("coach", coachPermissions);

        // admin 角色拥有所有权限
        rolePermissionsMap.put("admin", permissions);

        // 初始化角色并分配权限
        for (Map.Entry<String, List<Permission>> entry : rolePermissionsMap.entrySet()) {
            String roleName = entry.getKey();
            List<Permission> rolePermissions = entry.getValue();

            // 检查角色是否已存在
            Role existingRole = roleMapper.findByName(roleName);
            if (existingRole == null) {
                Role role = new Role(roleName);
                roleMapper.insert(role); // 插入新角色
                // 将角色与权限关联
                for (Permission permission : rolePermissions) {
                    rolePermissionMapper.insert(role.getid(), permission.getid());
                }
            }
        }
    }
}